package com.carrental.api.cms.controller;

import com.carrental.api.cms.domain.CmsCar;
import com.carrental.api.cms.domain.bo.CmsCarBo;
import com.carrental.api.cms.domain.vo.CmsCarVo;
import com.carrental.api.cms.service.ICmsCarService;
import com.carrental.common.response.CommonPage;
import com.carrental.common.response.CommonResult;
import com.carrental.api.common.query.CommonQueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 梁毅
 * @since 2022-03-02 23:26:10
 */
@RestController
@Api(tags = "车辆管理")
@RequestMapping("/cms/car")
public class CmsCarController {

    @Autowired
    private ICmsCarService carTestService;

    @PostMapping()
    @ApiOperation("添加车辆")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> carCreate(@RequestBody CmsCarBo car) {
        return CommonResult.success(carTestService.create(car));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除车辆信息")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> carRemove(@ApiParam(name = "id", value = "车辆标识", required = true)
                                           @PathVariable Long id) {
        return CommonResult.success(carTestService.delete(id));
    }

    @PutMapping("/{id}")
    @ApiOperation("修改车辆信息")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> carUpdate(
            @ApiParam(name = "id", value = "车辆标识", required = true)
            @PathVariable Long id, @RequestBody CmsCarBo car) {
        return CommonResult.success(carTestService.update(id, car));
    }

    @GetMapping()
    @ApiOperation("获取车辆列表")
    public CommonResult<List<CmsCar>> carList(CommonQueryParam commonQueryParam) {
        return CommonResult.success(carTestService.list(commonQueryParam));
    }

    @GetMapping("/store/{storeId}")
    @ApiOperation("获取车辆列表")
    public CommonResult<List<CmsCar>> carList(@PathVariable Long storeId,@RequestParam(required = false) String carName) {
        return CommonResult.success(carTestService.getCarListByStoreId(storeId,carName));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取车辆详情")
    public CommonResult<CmsCar> carObj(@PathVariable Long id) {
        return CommonResult.success(carTestService.detail(id));
    }

    @GetMapping("/page")
    @ApiOperation("获取车辆分页")
    public CommonResult<CommonPage<CmsCarVo>> carPage(CommonQueryParam commonQueryParam) {
        return CommonResult.success(CommonPage.restPage(carTestService.pageList(commonQueryParam)));
    }

}
